package com.jieshuibao.service;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.jieshuibao.common.config.Const;
import com.jieshuibao.common.config.Global;
import com.jieshuibao.common.config.enumtype.QuestionStatus;
import com.jieshuibao.mapper.AdvisoryMapper;
import com.jieshuibao.mapper.QuestionMapper;
import com.jieshuibao.mapper.WxRefundMapper;
import com.jieshuibao.model.db.Advisory;
import com.jieshuibao.model.db.Question;
import com.jieshuibao.model.db.WxRefund;
import com.jieshuibao.model.db.WxRefundExample;
import com.jieshuibao.model.domain.RefundProduct;
import com.jieshuibao.util.PayTypeUtil;
import com.jieshuibao.util.StringUtils;
import com.jieshuibao.wx.util.PayCommonUtil;

@Service
public class WxRefundService{
	
	private static final Logger logger = LoggerFactory
			.getLogger("WxRefundService.class");
	
	@Resource
	private WxRefundMapper wxRefundMapper;
	
	@Resource
	private QuestionMapper questionMapper;
	
	@Resource
	private RedisService redisService;
	
	@Resource
	private AdvisoryMapper advisoryMapper;
	
	@Resource
	private UserDealRecordService userDealRecordService;
	
	public void addWxRefund(WxRefund wxRefund) {
		wxRefundMapper.insertSelective(wxRefund);
	}
	
	public void delWxRefundByUuid(String uuid) {
		wxRefundMapper.deleteByPrimaryKey(uuid);
	}
	
	public void updateWxRefund(WxRefund wxRefund) {
		wxRefundMapper.updateByPrimaryKeySelective(wxRefund);
	}
	public void updateWxRefundByExample(WxRefund wxRefund,WxRefundExample wxRefundExample){
		wxRefundMapper.updateByExampleSelective(wxRefund, wxRefundExample);
	}
	public WxRefund getWxRefundByUuid(String uuid) {
		return wxRefundMapper.selectByPrimaryKey(uuid);
	}
	
	public List<WxRefund> getPageWxRefund(WxRefundExample wxRefundExample) {
		return wxRefundMapper.selectPage(wxRefundExample);
	}
	
	public int countWxRefund(WxRefundExample wxRefundExample) {
		return wxRefundMapper.countByExample(wxRefundExample);
	}
	
	
	//
	/**
	 * 微信退款订单
	 * 
	 * @param bsbWxRefund
	 * @param ref
	 *            status: FAIL失败 SUCCESS 成功
	 */
	public void addBsbRefundAndUpdateProduct(WxRefund WxRefund,
			RefundProduct ref) {
		if (WxRefund != null) {
			wxRefundMapper.insertSelective(WxRefund);
		}
		if (ref != null) {
			String uuid = ref.getUuid();
			String status = ref.getStatus();
			String goodsType = ref.getGoodsType();
			String errorMessage = ref.getErrorMessage();
			this.refundProduct(uuid, goodsType, status, errorMessage);
		}

	}

	// 处理微信退款 业务状态
	/**
	 * 
	 * @param uuid
	 *            业务id 必传
	 * @param goodsType
	 *            必传
	 * @param status
	 *            必传
	 * @param errorMessage
	 */

	public void refundProduct(String uuid, String goodsType, String status,
			String errorMessage) {
		if (StringUtils.isNotBlank(goodsType) && StringUtils.isNotBlank(uuid)) {

			if (goodsType.equals(PayTypeUtil.GOODSTYPE_REWARDBACK)) {
				Question question2 = new Question();
				question2.setUuid(uuid);
				if (status.equals(Global.FAIL)) {
					// 支付方式错误
					question2.setRemark(errorMessage);
					question2.setStatus(QuestionStatus.QU_STATUS_REFUNDEXCEPTION.getCode());
				} else if (status.equals(Global.SUCCESS)) {
					question2.setStatus(QuestionStatus.QU_STATUS_STOPREFUND.getCode());
				} else {
					question2.setRemark("参数异常");
					question2.setStatus(QuestionStatus.QU_STATUS_REFUNDEXCEPTION.getCode());
				}
				questionMapper.updateByPrimaryKeySelective(question2); // 结束悬赏
				redisService.delete(uuid + "_num");
			} else if (goodsType.equals(PayTypeUtil.GOODSTYPE_ADVISORYBACK)) {
				Advisory advisory = new Advisory();
				advisory.setUuid(uuid);
				if (status.equals(Global.FAIL)) {
					// 支付方式错误
					advisory.setType(Const.ADVISORY_STATUS_5);
					advisory.setRemark(errorMessage);
				} else if (status.equals(Global.SUCCESS)) {
					advisory.setType(Const.ADVISORY_STATUS_4);
				} else {
					advisory.setType(Const.ADVISORY_STATUS_5);
					advisory.setRemark("参数异常");
				}
				advisoryMapper.updateByPrimaryKeySelective(advisory);
			}
		}

	}
	//退款回调业务处理
	public void chuliWxRefund(WxRefund wxRefund,WxRefundExample wxRefundExample){
		try {
			//修改微信退款订单
			this.updateWxRefundByExample(wxRefund, wxRefundExample);
			
			String uuid = wxRefund.getUuid();
			String refund_status = wxRefund.getRefund_status();
			String err_code_des = wxRefund.getErr_code_des();
			
			
			WxRefund wxRefund2 = wxRefundMapper.selectByPrimaryKey(uuid);
			if (wxRefund2!=null) {
				String goodsType = wxRefund2.getGoodsType();
				String productId = wxRefund2.getProductId();
				Double refund_fee = wxRefund2.getRefund_fee();
				String userId = wxRefund2.getUserId();
				if (refund_status.equals(PayCommonUtil.REFUND_STATUS_1)) {
					//成功 添加流水
					userDealRecordService.addUserCompanyUserDealRecord(productId, goodsType, refund_fee, userId, PayTypeUtil.PAYTYPE_WEIXIN);
					logger.info("微信退款回调成功并退款状态为成功,添加流水");
				}else {
					//失败
					this.refundProduct(uuid, goodsType, Global.FAIL, "微信退款失败："+err_code_des);
					logger.info("微信退款回调成功，退款失败");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("微信退款处理订单错误！");
		}
		
	}
	
}
